package com.mini.demos.webgame.handler;

import com.mini.demos.webgame.db.DBUtils;
import com.mini.demos.webgame.msg.MsgHead;
import com.mini.demos.webgame.msg.RegMsg;
import com.mini.demos.webgame.msg.RegMsgResp;
import com.mini.demos.webgame.util.CommandConstants;
import com.mini.demos.webgame.util.MsgSequenceId;
import com.mini.demos.webgame.util.Utils;
import com.mini.framework.communicate.mina.handler.Handler;
import com.mini.framework.communicate.mina.protocol.Msg;

public class RegHandler extends Handler {

	@Override
	public void handle(Msg msg) {
		//注册信息保存.
		RegMsg regMsg = (RegMsg)msg;
		String sql = "insert into t_user(loginName,password,displayName) values(?,?,?)";
		Object[] params = new Object[]{regMsg.getUserName(),regMsg.getUserPwd(),regMsg.getNickName()};
		DBUtils db = new DBUtils();
		db.save(sql, params);
		
		//返回.
		RegMsgResp resp = new RegMsgResp();
		String codeMsg = "Success";
		resp.setCode(Integer.valueOf(0));
		resp.setMsg(codeMsg);
		
		MsgHead head = new MsgHead();
		head.setVersion((byte)0x01);
		head.setCommandId(CommandConstants.REG_RESP);
		head.setSeqId(MsgSequenceId.getInstance().getSequenceId());
		Integer totalLength = CommandConstants.HEAD_LENGTH + 4 + codeMsg.getBytes().length;
		head.setTotalLength(totalLength.intValue());
		head.setTime(Utils.getTime());
		head.setSpace(0);
		resp.setHead(head);
		
		msg.getSender().send(resp);
	}

}
